﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Diagnostics;
using VietnameseHelper;
using Assergs.Windows;
using ViCode_LeVi.Data;
using System.Windows.Controls;
using ViCode_LeVi.Langs;
using Assergs.Windows.Tests;
using FES.Intimex.Client.Shared.Splash;
using ViCode_LeVi.KeyGen;
using ViCode_LeVi.Controls;
using ViCode_LeVi.Engines;
using Client;

namespace ViCode_LeVi
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup(object sender, StartupEventArgs e)
        {
            #region Just one  instance run
            if (Debugger.IsAttached == false)
            {
                Process thisProcess = Process.GetCurrentProcess();
                foreach (Process p in Process.GetProcessesByName(thisProcess.ProcessName))
                {
                    if (p.Id != thisProcess.Id)
                    {
                        // Shut Down
                        Application.Current.Shutdown();
                        return;
                    }
                }
            }
            #endregion
            Splasher.ShowSplash();
            LangManager.Run();
            XceedHelper.XceedHelper.InitializeLicense();
            ColorationWindow.PATH_Skin = FileFolder.AppInfo.AppDirectory + "/Skin.vmconfig";
            ColorationWindow.LoadSkin();
            this.Resources = new OfficeStyle();
            EventManager.RegisterClassHandler(typeof(TextBox),
                  TextBox.LostFocusEvent,
                  new RoutedEventHandler(TextBox_LostFocus));
            EngineAutoSave.StartEngine();
            RegisterHelper.InsertClient();
        }
        void TextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            TextBox txt = sender as TextBox;
            if (txt != null)
            {
                ThreadHelper.Runner.RunAfterUIAction(txt, () =>
                {
                    var be = txt.GetBindingExpression(TextBox.TextProperty);
                    if (be != null)
                    {
                        if (
                            (be.ParentBinding.Mode == System.Windows.Data.BindingMode.Default
                            || be.ParentBinding.Mode == System.Windows.Data.BindingMode.TwoWay)
                            && be.ParentBinding.UpdateSourceTrigger != System.Windows.Data.UpdateSourceTrigger.Explicit)
                        {
                            be.UpdateSource();
                            be.UpdateTarget();
                        }
                    }
                }, System.Windows.Threading.DispatcherPriority.Send);
            }
        }
    }
}
